package com.huluobo.a2109mvpimdemo.utls;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

/**
 * Created by LC on 2023/11/25.
 * 将一个字符串转换成一个32位的加密字符串,可以验证,通过相同字符串通过md5加密后,产生新字符串相等
 */
public class MD5Utils {
    public static String encodeByMd5(String str) {
        MessageDigest md5 = null;

        try {
            md5 = MessageDigest.getInstance("MD5");
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                String newStr = Base64.getEncoder().encodeToString(md5.digest(str.getBytes("utf-8")));
                return newStr;
            }
        } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
}
